/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.lang.Exception.*;

/**
 *
 * @author Shelley Co
 */
public class ActionSlots 
{
    private dbConnection dbcon = new dbConnection();
    private String characterName, actionName, type, content;
    private int slot, weight;
    
    public ActionSlots(){}
    
    public ActionSlots (String cname, String sname, String cont, String t, int s, int wt)
    {
        setCharacterName(cname);
        setActionName(sname);
        setType(t);
        setSlot(s);
        setWeight(wt);
        setContent(cont);
    }
    
    public void deleteSlotsByChar (String cname)
    {
        String query;		
    	ResultSet rs;   
        PreparedStatement stmt;
        
       	getDbcon().connect();						
        query = "DELETE FROM ActionSlots WHERE characterName = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);	
            stmt.setString(1, cname);
            stmt.executeUpdate();				
            stmt.close();						
        } 
        catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
     }
     
    public void addActionSlot (ActionSlots a)
    {		
        String query; 		
        PreparedStatement stmt;
        
        getDbcon().connect();
        query = "INSERT INTO ActionSlots (actionName, characterName, slotNo, actionType, actionContent, weight) VALUES (?,?,?,?,?,?)";

        try 
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, a.getActionName());
            stmt.setString(2, a.getCharacterName());
            stmt.setInt(3, a.getSlot());
            stmt.setString(4, a.getType());
            stmt.setString(5, a.getContent());
            stmt.setInt(6, a.getWeight());
            stmt.executeUpdate();	
            stmt.close();
        } 
        catch (Exception ex) 
        {
            System.out.println(ex.getMessage());
        }
        getDbcon().disconnect();
    }
    
    public void addMember (String cname)
    {		
        String query; 		
        PreparedStatement stmt;
        
        getDbcon().connect();
        query = "INSERT INTO ActionSlots (actionName, characterName, slotNo, actionType, actionContent, weight) VALUES (?,?,?,?,?,?)";

        for (int i=1; i<5; i++)
        {
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, "Normal Attack");
                stmt.setString(2, cname);
                stmt.setInt(3, i);
                stmt.setString(4, "skill");
                stmt.setString(5, "Normal Attack");
                stmt.setInt(6, 1);
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
        }
        getDbcon().disconnect();
    }
    
    public void editSlot (String character, String sname, String cont, String t, int isEq , int w)
    {
        String query;
        PreparedStatement stmt;
        getDbcon().connect();
        query = "UPDATE ActionSlots SET actionName = ?, actionType = ?, actionContent = ?, weight = ? WHERE characterName = ? AND slotNo = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, sname); 
            stmt.setString(2, t);
            stmt.setString(3, cont);      
            stmt.setInt(4, w);
            stmt.setString(5, character);      
            stmt.setInt(6, isEq);
            stmt.executeUpdate();
            stmt.close();					
        } 
        catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
    }
    
    public void editContent(String character, int isEq, String cont, String t)
    {
        String query;
        PreparedStatement stmt;
        getDbcon().connect();
        query = "UPDATE ActionSlots SET actionContent = ?, actionName = ?, actionType = ?, weight = ? WHERE characterName = ? AND slotNo = ?";
        
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cont);
            stmt.setString(2, "");
            stmt.setString(3, t);
            stmt.setInt(4, 0);
            stmt.setString(5, character);
            stmt.setInt(6, isEq);
            stmt.executeUpdate();
            stmt.close();					
        } 
        catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
    }
        
    public Vector viewEquipedSkills (String cname)
    {
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<ActionSlots> sList;
            
        sList = new Vector<ActionSlots>();
                
        getDbcon().connect();
        query = "SELECT actionName, characterName, actionType, slotNo, actionContent, weight FROM ActionSlots WHERE characterName = ?";
        System.out.println("-p--------------------------view");
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                ActionSlots s = new ActionSlots();
                //System.out.println("-p--------------------------view"+rs.getString("actionName"));
                
                s.setActionName(rs.getString("actionName").trim());
                s.setCharacterName(rs.getString("characterName").trim());
                s.setType(rs.getString("actionType").trim());
                s.setSlot(rs.getInt("slotNo"));
                s.setContent(rs.getString("actionContent").trim());
                s.setWeight(rs.getInt("weight"));
                
                sList.add(s);
             }
            
            rs.close(); 					
            stmt.close();
            return sList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
    }
    
    public boolean isHere (int num, String cname)
    {
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean bHere = false;
                
        getDbcon().connect();
        query = "SELECT actionName, characterName, actionType, slotNo, actionContent, weight FROM ActionSlots WHERE slotNo = ? AND characterName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setInt (1, num);
            stmt.setString (2, cname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                this.setActionName(rs.getString("actionName").trim());
                this.setCharacterName(rs.getString("characterName").trim());
                this.setSlot(rs.getInt("slotNo"));
                this.setType(rs.getString("actionType").trim());
                this.setContent(rs.getString("actionContent").trim());
                this.setWeight(rs.getInt("weight"));
                bHere = true;
            }
            rs.close(); 					
            stmt.close();
        } 
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally 
        {
            getDbcon().disconnect();
            return bHere;
        }
    }
    
    public boolean checkExist(String cname, String act) 
    {
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
        
        getDbcon().connect();
        query = "SELECT * FROM ActionSlots WHERE characterName = ? AND actionName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cname);
            stmt.setString(2, act);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                exist = true;
            }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }

    public String getCharacterName() {
        return characterName;
    }

    public void setCharacterName(String characterName) {
        this.characterName = characterName;
    }

    public String getActionName() {
        return actionName;
    }

    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public int getSlot() {
        return slot;
    }

    public void setSlot(int slot) {
        this.slot = slot;
    }

    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int wt) {
        this.weight = wt;
    }
}

//    public void editSlotNo (String character, String sname, int isEq, String t)
//    {
//        String query;
//        PreparedStatement stmt;
//        getDbcon().connect();
//        query = "UPDATE ActionSlots SET slotNo = ? WHERE characterName = ? AND actionName = ? AND actionType = ?";
//        
//        try 
//        {
//            stmt = getDbcon().con.prepareStatement(query);      
//            stmt.setInt(1, isEq);
//            stmt.setString(2, character);
//            stmt.setString(3, sname); 
//            stmt.setString(4, t);
//            stmt.executeUpdate();
//            stmt.close();					
//        } 
//        catch (Exception ex) 
//        {
//            System.out.println(ex.getMessage());	       	
//        }
//        getDbcon().disconnect();
//    }